package com.zhongyi.dao;

import com.zhongyi.domain.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import static org.junit.Assert.*;

public class UserDaoTest {

    @Test
    public void findAll() {
        try {
            InputStream in = Resources.getResourceAsStream("sqlMapConfig.xml");
            SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
            SqlSession session = factory.openSession();
            UserDao userDao = session.getMapper(UserDao.class);
            List<User> users = userDao.findAll();
            for(User user:users){
                System.out.println(user);
            }
            session.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void saveUser() {
        User user = new User();
        user.setName("赵六");
        user.setMoney(2000d);

        try {
            InputStream in = Resources.getResourceAsStream("sqlMapConfig.xml");
            SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
            SqlSession session = factory.openSession();
            UserDao userDao = session.getMapper(UserDao.class);
            userDao.saveUser(user);
            session.commit();
            session.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
